package com.centriole.service.workflow.controller.ex;

import com.centriole.common.log.LogUtils;
import com.centriole.common.utils.CommonUtil;
import com.centriole.common.utils.PaasResult;
import com.centriole.service.workflow.common.QueryVo;
import com.centriole.service.workflow.dao.ProInstanceMapper;
import com.centriole.service.workflow.entity.ProInstance;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  功   能：  针对 工作流监控web提供的接口
 *  创建人：陈默晗
 *  时   间：2018年5月4日下午4:12:28
 */
@RestController
@RequestMapping("/pms")
public class PmsController {

    @Autowired
    ExService exService;

    @Autowired
    ProInstanceMapper proInstanceMapper;

    /**
     * 查询当前进行中的流程 ， 以appName进行分组展示
     * @param vo    查询条件
     * @return  List<ProInstance>
     */
    @RequestMapping("/listMap.json")
    public PaasResult<List<Pair<String,List<ProInstance>>>>  listOnGoing(QueryVo vo) {
        return PaasResult.success(
                exService.listInstanceByCondition( vo )
                        .parallelStream()
                        .collect(Collectors.groupingBy(e->e.getAppName()))
                        .entrySet().parallelStream()
                        .map(e->new Pair<>( ExUtils.ifBlankThenDefaultKey(e.getKey()) ,e.getValue()))
                        .sorted( (e1,e2)->e2.getValue().size() - e1.getValue().size() )
                        .collect(Collectors.toList())
        );
    }

    /**
     * 对当前进行中的流程【数量】，以appName进行分组展示
     * @param vo    查询条件
     * @return  List<Pair<String,Integer>>
     */
    @RequestMapping("/countMap.json")
    public  PaasResult<List<Pair<String,Integer>>> countOnGoing(QueryVo vo){
        return PaasResult.success(
                exService.listInstanceByCondition( vo )
                        .parallelStream()
                        .collect(Collectors.groupingBy(e->e.getAppName()))
                        .entrySet().parallelStream()
                        .map(e->new Pair<>( ExUtils.ifBlankThenDefaultKey(e.getKey()), e.getValue().size() ))
                        .sorted( (e1,e2)->e2.getValue() - e1.getValue() )
                        .collect(Collectors.toList())
        );
    }

    /**
     * 工作流运行时 基本数据 ：包含了 运行时流程数量、已结束流程数量、长时间未结束的流程数量、今天新增数量
     * @return
     */
    @RequestMapping("/basicInfo.json")
    public PaasResult<Map<String,String>> basicInfo(){
        List<ProInstance> proInstances = exService.listInstanceByCondition(new QueryVo());
        Map<String, Integer> basicInfoMap = proInstances.stream()
                .collect(Collectors.groupingBy(ProInstance::getStateName))
                .entrySet().stream()
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().size()));
        LinkedHashMap result = new LinkedHashMap(6, 1.0f , true);
        result.putAll(basicInfoMap);
        result.put("timeout",  exService.listInstanceByCondition(new QueryVo(30)).size() );
        result.put("new",exService.listTodayInstaces().size());

        return PaasResult.success("ok",result);
    }

    /**
     * 查询
     * @param vo 查询条件
     * @return  List<Pair<String,Integer>>
     */
    @RequestMapping("/list.json")
    public PaasResult<List<ProInstance>> listByCondition(QueryVo vo){
    //    System.err.println("分页的条件： "+ "page=" + vo.getPage() + " , size= " + vo.getLimit());
        List<ProInstance> proInstances = exService.listInstanceByCondition(vo);
    //    proInstances.forEach(System.out::println);
        List<ProInstance> page = CommonUtil.page(proInstances, vo.getPage(), vo.getLimit());
   //     System.out.println("最后的数据大小: " + page.size());
        return PaasResult.successWithCode0("ok", page , proInstances.size());
    }

    /**
     * 根据实例id查询
     * @param instanceId 实例id
     * @return  List<Pair<String,Integer>>
     */
    @RequestMapping("/instance.json")
    public PaasResult<ProInstance> getProInstanceByCondition(String instanceId){
        ProInstance proInstance = proInstanceMapper.selectByInstanceId(instanceId);
        return PaasResult.success(proInstance);
    }

    @RequestMapping("/deadInstance.json")
    public PaasResult<List<ProInstance>> listDeadProInstances(QueryVo vo){
        List<ProInstance> listPaasResult = exService.listDeadProInstances(vo);
        List<ProInstance> page = CommonUtil.page(listPaasResult, vo.getPage(), vo.getLimit());
        return PaasResult.successWithCode0("ok", page,listPaasResult.size() );
    }

    @RequestMapping("/test")
    public String testException(){
       return exService.testError();
    }
}
